package com.ychs.controller.front;

import com.ychs.bean.Seat;
import com.ychs.bean.User;
import com.ychs.service.ISeatService;
import com.ychs.service.IUserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


/**
 * Description: 描述【】
 * Copyright: Copyright (c) 2018
 * Company: 英才汇硕信息技术有限公司
 *
 * @author tianchuanjie
 * @version 1.0
 * @created 2018/12/24 16:53
 */
@Controller
public class SeatFrontController {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private ISeatService seatService;

    @Autowired
    private IUserService userService;

    @GetMapping("frontSeat")
    public String librarySeatInfo(String clazzName, HttpServletRequest request, HttpSession session) {
        User user = (User) session.getAttribute("userInfo");
        String userWorkId = user.getUserWorkId();
        //根据用户名查询用户预约状态
        User user1 = userService.queryUserStatus(userWorkId);
        int userSeatStatus = user1.getUserSeatStatus();
        request.setAttribute("userSeatStatus", userSeatStatus);

        //根据教室名称查询教室的座位数
        request.setAttribute("clazzName", clazzName);
        List<Seat> seatList = seatService.queryByClazzName(clazzName);

        logger.debug(seatList.size());
        int seatNum = seatList.size();
        request.setAttribute("seatNum", seatNum);

        int quotient = seatNum / 8;
        int remainder = seatNum % 8;

        //座位套数整数
        if (remainder == 0 && quotient != 0) {
            request.setAttribute("seatGroup", quotient);
        } else if (remainder != 0 && quotient != 0) {
            //座位套数大于1  不整数
            request.setAttribute("seatGroup", quotient + 1);
        } else if (remainder != 0 && quotient == 0) {
            //座位套数1  但是不够一套
            request.setAttribute("seatGroup", 1);
        } else if (remainder == 0 && quotient == 0) {
            //没座位 但是教室开放的
            request.setAttribute("seatGroup", 0);
        }

        //每8个进行封装  不够8个的话 也封装


        Seat[] array = seatList.toArray(new Seat[seatList.size()]);
        request.setAttribute("SeatArray", array);

        return "/front/seat";
    }


    /**
     * 释放座位
     *
     * @param userNameSeat
     * @param clazzName
     * @return
     */
    @GetMapping("/releaseSeat")
    public String releseSeat(String userNameSeat, String clazzName) {
        //释放预约的座位  通过修改座位表数据  清除座位信息
        int result = seatService.updateSeat(userNameSeat);
        //修改用户预约状态 为0
        int result1 = userService.updateUser(userNameSeat);
        return "redirect:frontSeat?clazzName=" + clazzName;
    }

    /**
     * 预定座位
     *
     * @param seatName     座位名
     * @param clazzName    教室名
     * @param userNameSeat 用户名
     * @return
     */
    @GetMapping("/appointmentSeat")
    public String appointmentSeat(String userNameSeat, String seatName, String clazzName) {
        //查询判断该座位是否被预约
        Seat seat = seatService.querySeatStatus(seatName, clazzName);
        //座位状态
        int seatStatus = seat.getSeatStatus();

        //如果未预约
        if (seatStatus == 0) {
            //预约教室座位
            int result = seatService.appointmentSeat(userNameSeat, seatName, clazzName);
            //修改用户预约状态  为1
            int result1 = userService.appointmentUser(userNameSeat);
        }

        return "redirect:frontSeat?clazzName=" + clazzName;

    }

    /**
     * 更换座位
     * @param seatName     座位名
     * @param clazzName    教室名
     * @param userNameSeat 用户名
     * @return
     */
    @GetMapping("/replaceSeat")
    public String replaceSeat(String userNameSeat,String clazzName ,String seatName) {

        //先通过用户查询已经预约的座位 释放
        int result = seatService.updateSeat(userNameSeat);
        //释放用户信息
        int result1 = userService.updateUser(userNameSeat);
        //查询判断该座位是否被预约
        Seat seat = seatService.querySeatStatus(seatName, clazzName);
        //座位状态
        int seatStatus = seat.getSeatStatus();

        //如果未预约
        if (seatStatus == 0) {
            //预约教室座位
            int result2 = seatService.appointmentSeat(userNameSeat, seatName, clazzName);
            //修改用户预约状态  为1
            int result3 = userService.appointmentUser(userNameSeat);
        }

        return "redirect:frontSeat?clazzName=" + clazzName;
    }

}
